#include"Stack.h"

void StackInit(ST* ps)
{
    assert(ps);
    ps->a = NULL;
    ps->top = ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
    assert(ps);
    if(ps->a != NULL)
    {
        free(ps->a);
        ps->a = NULL;
        ps->capacity  = ps->top = 0;
    }
}
void StackPush(ST* ps, STDataType x)
{
    assert(ps);
    if(ps->capacity == ps->top)
    {
        int newcapacity = ps->capacity == 0 ? 4 :ps->capacity * 2;
        STDataType* tem = (STDataType*)realloc(ps->a, sizeof(STDataType)* newcapacity);
        if(tem == NULL)
        {
            perror("realloc");
            exit(-1);
        }
        ps->a = tem;
        ps->capacity = newcapacity;
    }
    ps->a[ps->top] = x;
    ps->top++;
}
void StackPop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));

    ps->top--;

}
STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));//空栈没有顶
    return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
    assert(ps);
    return ps->top;
}
bool StackEmpty(ST* ps)
{
    assert(ps);
    return ps->top == 0;//
}
